#{extends 'backend_main.html' /}
#{set title:'后台管理' /}
<div class="x-body">
    <form class="layui-form">
        <input type="hidden" id="filename" name="filename" lay-verify="filename">
        <div class="layui-form-item">
            <label for="customer_name" class="layui-form-label">
                <span class="x-red">*</span>导入数据
            </label>
            <div class="layui-input-inline">
                <button type="button" class="layui-btn" id="uploadExcel">
                    <i class="layui-icon">&#xe67c;</i>选择Excel
                </button>
            </div>
            <div class="layui-input-inline">
                <span  class="layui-btn layui-btn-primary" id="fileName0"></span>
            </div>
        </div>
        <div class="layui-form-item">

        </div>
        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label">
            </label>
            <button  class="layui-btn" lay-filter="add" lay-submit="">
                导入数据库
            </button>
        </div>
        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label">
            </label>
            <a href="/public/模版1.xls" class="layui-btn layui-btn-danger">下载 模版1.xls </a>
        </div>
        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label">
            </label>
            <span class="layui-btn layui-btn-primary layui-btn-xs">备注:导入的文件格式务必跟模版1.xls 格式相同，否则会出错，请勿乱操作哦</span>
        </div>
    </form>
</div>
<script>
    layui.use(['form','layer','upload','laydate'], function(){
        var $ = layui.jquery;
        var form = layui.form,layer = layui.layer;
        var upload = layui.upload;
        //执行实例
        upload.render({
            elem: '#uploadExcel' //绑定元素
            ,url: '/FileController/uploadImage' //上传接口
            ,field:"imgFile"
            ,accept:"file"
            ,exts:"xls"
            ,drag:true
            ,done: function(res){
                //上传完毕回调
                $("#filename").val(res.filename);
                console.log(JSON.stringify(res));
                $("#fileName0").html(res.file_realname);
            }
        });
        //自定义验证规则
        form.verify({
            filename: function(value) {
                if (value.length < 2) {
                    return '先选择导入文件啊';
                }
            }
        });
        //监听提交
        form.on('submit(add)', function(data){
            console.log(JSON.stringify(data.field));
            //发异步，把数据提交给php
            $.ajax({
                type:"POST",                      //请求类型
                url:"./orderEdit?v=importExcel",                 //URL
                data:data.field,                  //传递的参数
                dataType:"json",                 //返回的数据类型
                success:function(data){          //data就是返回的json类型的数据
                    console.log(data);
                    layer.alert(data.msg, {icon: 6},function () {
                        parent.window.location.reload();
                    });
                }
            });
            return false;
        });
    });
</script>
